{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banner_Top_06.06.18.jpg?raw=true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Basic math operations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From the [International Math modelling challenge](https://immchallenge.org/Contests/index.html), looking at the [winning paper](https://immchallenge.org/Contests/2017/2017057.pdf) for 2017 [Jet lag problem](https://immchallenge.org/Contests/2017/Problem.html), this is productivity metric that was used:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](images/question.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As part of this notebook we will demonstrate some basic  math calculations in Python, calculating the  first part of this metric - Jet lag Component:\n",
    "\n",
    "$$ J  = \\sqrt{\\frac{1}{|M|}\\sum_{m=1}^{M}P_{m}^2 }$$\n",
    "\n",
    "$$ P_{m} = min\\big\\{ 10,\\;10+6\\; \\cos \\big( \\frac{\\pi\\Delta J_{m}}{12}-\\frac{\\pi}{24}  \\big) \\big\\} $$\n",
    "We will  use random input data just for demonstration purposes.  \n",
    "\n",
    "We will write formulas in Jupyter notebook markdown cells using LateX   \n",
    "(Basic LateX tutorial is located  at **getting-started/tutorials/JupyterTestdrive-LateX.ipynb**,\n",
    " Latex cheat sheet is [here](http://tug.ctan.org/info/symbols/comprehensive/symbols-a4.pdf))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import the numpy library\n",
    "[NumPy](http://www.numpy.org) is the fundamental package for scientific computing with Python.  \n",
    "[quick-start tutorial](https://docs.scipy.org/doc/numpy-1.15.1/user/quickstart.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 1\n",
    "First we calculate $P_{m}$ from this formula:\n",
    "$$ P_{m} = \\min\\left\\{ 10,\\;10+6\\; \\cos \\left( \\frac{\\pi\\Delta J_{m}}{12}-\\frac{\\pi}{24}  \\right) \\right\\} $$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We need to define number of members M. Lets set it to 4."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "M = 4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Vectors and matricies\n",
    "(Calculating  $\\Delta J_{m}$)  \n",
    "$\\Delta J_{m}$ is shift in time zones for a given memeber m.   \n",
    "To demonstrate how to work with vectors -  we will define origin time : **time1** and destination time: **time2** for every memeber, subract them and calculate the delta. \n",
    " \n",
    "To create vectors we will use [np.array()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html)\n",
    "It can be used to create 1- and multi-dimensional arrays."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'> [1 2 3 4]\n",
      "<class 'numpy.ndarray'> [1 1 1 0]\n"
     ]
    }
   ],
   "source": [
    "#just randomly assigning times - vectors have size 4 - number of members M\n",
    "time1 = np.array([1,2,3,4])\n",
    "time2 = np.array([1,1,1,0])\n",
    "print(type(time1), time1)\n",
    "print(type(time2), time2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To  subtract one vector from another we can just minus sign.  \n",
    "[Other numerical operations on arrays](http://scipy-lectures.org/intro/numpy/operations.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 4]\n"
     ]
    }
   ],
   "source": [
    "delta = time1 - time2\n",
    "print(delta)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Working with 2 dimentions,it's handy to use [np.matrix()](https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.matrix.html). More examples of ndarrays and matricies [here](https://www.python-course.eu/matrix_arithmetic.php)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pi and other constants\n",
    "Going back to the formula: $$ P_{m} = \\min\\left\\{ 10,\\;10+6\\; \\cos \\left( \\frac{\\pi\\Delta J_{m}}{12}-\\frac{\\pi}{24}  \\right) \\right\\}, $$  to calculate $P_{m}$ we need to $\\pi$, cos  and min."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**$\\pi$** is defined as constant **np.pi**   \n",
    "[Full list of NumPy constants](https://docs.scipy.org/doc/numpy/reference/constants.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.141592653589793\n"
     ]
    }
   ],
   "source": [
    "print(np.pi)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Math functions\n",
    "[Full list of NumPy math functions](https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.math.html)\n",
    "#### Cosine\n",
    "(Calculating $ 6\\; \\cos \\left( \\frac{\\pi\\Delta J_{m}}{12}-\\frac{\\pi}{24}  \\right)  $ )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To calculate cosine we will use [np.cos()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.cos.html) (as input it takes values in radians).  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5.94866917 5.94866917 5.5432772  3.65256857]\n"
     ]
    }
   ],
   "source": [
    "first_part=6 * np.cos(np.pi * delta / 12 - np.pi / 24)\n",
    "print(first_part)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Minimum.\n",
    "(Calculating $\\min\\left\\{ 10,\\;10+6\\; \\cos \\left( \\frac{\\pi\\Delta J_{m}}{12}-\\frac{\\pi}{24}  \\right) \\right\\}$)  \n",
    "We will use [np.minimum()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.minimum.html) to calcutae minimum."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10. 10. 10. 10.]\n"
     ]
    }
   ],
   "source": [
    "p=np.minimum(10, 10 + first_part)\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Recap\n",
    "We have calculated the first part of the Jet Lag Component: $$ P_{m} = \\min\\left\\{ 10,\\;10+6 \\; \\cos \\left( \\frac{\\pi\\Delta J_{m}}{12}-\\frac{\\pi}{24}  \\right) \\right\\} $$ using following code and assigning random initial values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10. 10. 10. 10.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "M=4\n",
    "time1 = np.array([1,2,3,4])\n",
    "time2 = np.array([1,1,1,0])\n",
    "delta=time1-time2\n",
    "first_part=6*np.cos(np.pi*delta/12-np.pi/24)\n",
    "p=np.minimum(10,10+first_part)\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 2\n",
    "Next we are going to calculate the second part of Jet Lag Component: $$ J  = \\sqrt{\\frac{1}{|M|}\\sum_{m=1}^{M}P_{m}^2 }$$\n",
    "To do it we need to find way how to calculate power, absolute value, summary and square root."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Power\n",
    "(Calculating $P_{m}^2$)    \n",
    "To calculate power of two for the vector - we can use [np.power()](https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.power.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[100. 100. 100. 100.]\n"
     ]
    }
   ],
   "source": [
    "p_power2=np.power(p, 2)\n",
    "print(p_power2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Alternatively, we can use a common Python for powers denoted with two asterix like `**`. This is shown below"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[100. 100. 100. 100.]\n"
     ]
    }
   ],
   "source": [
    "p_power2 = p**2\n",
    "print(p_power2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Summary\n",
    "(Calculating $\\sum_{m=1}^{M}P_{m}^2$)   \n",
    "To calculate summary of all the elements in vector, we are going to be using [np.sum()](https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.sum.html) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "400.0\n"
     ]
    }
   ],
   "source": [
    "J=np.sum(p_power2)\n",
    "print(J)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Absolute value\n",
    "To calculate $|M|$  -  absolute value of M - we are going to use [np.absolute()](https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.absolute.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "M_abs=np.absolute(M)\n",
    "print(M_abs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Square root\n",
    "(Calculating $\\sqrt{\\frac{1}{|M|}\\sum_{m=1}^{M}P_{m}^2 }$)  \n",
    "[np.sqrt()](https://docs.scipy.org/doc/numpy-1.9.2/reference/generated/numpy.sqrt.html) is used to calculate square root."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.0\n"
     ]
    }
   ],
   "source": [
    "J = J * (1 / M_abs)\n",
    "J = np.sqrt(J)\n",
    "print(J)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Recap:\n",
    "We have calculated Jet Lag component\n",
    "$$ J  = \\sqrt{\\frac{1}{|M|}\\sum_{m=1}^{M}P_{m}^2 }$$\n",
    "\n",
    "$$ P_{m} = \\min\\left\\{ 10,\\;10+6cos \\left( \\frac{\\pi\\Delta J_{m}}{12}-\\frac{\\pi}{24}  \\right) \\right\\} $$\n",
    "using following code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "##part1\n",
    "M=4\n",
    "time1 = np.array([1,2,3,4])\n",
    "time2 = np.array([1,1,1,0])\n",
    "delta=time1-time2\n",
    "first_part=6*np.cos(np.pi*delta/12-np.pi/24)\n",
    "p=np.minimum(10,10+first_part)\n",
    "\n",
    "##part2\n",
    "p_power2=np.power(p, 2)\n",
    "J=np.sum(p_power2)\n",
    "J=np.sqrt(J*(1/np.absolute(M)))\n",
    "print(J)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try calculating the second part of the metric  - distance component D\n",
    "$$ D  = \\frac{C_{\\bigoplus}\\sqrt{|M|}}{\\sqrt{\\sum_{l=1}^{M}d_{l}^2 }}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$C_{0}$ is the circumference of the Earth. We are using [scientific notation](https://chortle.ccsu.edu/java5/Notes/chap11/ch11_5.html) $40.075e3$  which means $40.075*10^3$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "40075.0\n"
     ]
    }
   ],
   "source": [
    "C0=40.075e3 # kilometers\n",
    "print(C0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$d_{l}$ are the distances traveled by individual members. Initialize it randomly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "### your code here "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6076.155819284911\n"
     ]
    }
   ],
   "source": [
    "d = np.array([5,6,7,8])\n",
    "numerator = C0 * np.sqrt(np.absolute(M))\n",
    "denominator = np.sqrt(np.sum(np.power(d, 2)))\n",
    "D= numerator / denominator\n",
    "print(D)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banners_Bottom_06.06.18.jpg?raw=true)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "nbTranslate": {
   "displayLangs": [
    "*"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "fr",
   "useGoogleTranslate": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
